package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: dw
 * @Date 2022/3/16 23:48
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired(required = false)
    private HttpSession session;

    /**
     * 根据用户id查询购物车内所有物品
     *
     * @return
     */
    @Override
    public List<ShoppingCart> findAll() {
        //获取会话域的用户id
        Long id = (Long) session.getAttribute("user");

        //调用mapper层查询
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findAll(id);

        //返回数据
        return shoppingCartList;
    }

    /**
     * 增加菜品到购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    @Transactional
    public ShoppingCart save(ShoppingCart shoppingCart) {

        //调用mapper传userId查询
        ShoppingCart dbShoppingCart = shoppingCartMapper.findByShoppingCart(shoppingCart);

        //判断该菜品或套餐是否存在
        if (dbShoppingCart!=null) {
            //存在将数据库的number+1
            dbShoppingCart.setNumber(dbShoppingCart.getNumber()+1);
            //调用mapper更新数据
            shoppingCartMapper.updateNumber(dbShoppingCart);
        } else {
            //没有则新增一条数据
            dbShoppingCart = shoppingCart;
            //补全创建时间
            dbShoppingCart.setCreateTime(LocalDateTime.now());
            //将number设置为1
            dbShoppingCart.setNumber(1);

           //调用mapper层添加
            shoppingCartMapper.save(dbShoppingCart);
        }
        return dbShoppingCart;
    }

    /**
     * 根据用户id清空购物车
     *
     * @return
     */
    @Override
    public boolean deleteByUserId() {
        //因为前端没传，所以获取会话域中登录用户id
        Long userId = (Long) session.getAttribute("user");
        //调用mapper清空购物车
        shoppingCartMapper.delete(userId);
        return true;
    }

    /**
     * 减少购物车菜品或套餐
     *
     * @param shoppingCart
     * @return
     */
    @Override
    @Transactional
    public boolean reduce(ShoppingCart shoppingCart) {
        //调用mapper传userId查询
        ShoppingCart dbShoppingCart = shoppingCartMapper.findByShoppingCart(shoppingCart);

        //判断该菜品或套餐是否存在
        if (dbShoppingCart!=null) {
            //存在将数据库的number-1
            dbShoppingCart.setNumber(dbShoppingCart.getNumber()-1);
            //调用mapper更新数据
            shoppingCartMapper.updateNumber(dbShoppingCart);

            if (dbShoppingCart.getNumber()==0) {
                shoppingCartMapper.deleteByDishIdOrSetmealId(shoppingCart);
            }
        }
        return true;
    }


}
